package com.lijiajian.interceptor;

import com.lijiajian.Service.UserService;
import com.lijiajian.exceptions.NoLoginException;
import com.lijiajian.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NoLoginInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private UserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        if(userId==-1||userService.selectByPrimaryKey(userId)==null){
            throw new NoLoginException();
        }
        return true;
    }
}
